package com.nari.ncps.screen.service.impl.electric;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.nari.ncps.screen.mapper.electric.ElectricProdoperCoalConsumeMapper;
import com.nari.ncps.screen.service.electric.ElectricProdoperCoalConsumeService;
import com.nari.ncps.screen.vo.electric.CoalConsumeTrendDto;
import com.nari.ncps.screen.entity.electric.ElectricProdoperCoalConsume;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 供电煤耗趋势表 服务实现类
 * </p>
 *
 * @author xuesong
 * @since 2025-08-23
 */
@Service
public class ElectricProdoperCoalConsumeServiceImpl extends ServiceImpl<ElectricProdoperCoalConsumeMapper, ElectricProdoperCoalConsume> implements ElectricProdoperCoalConsumeService {


    /**
     * 供电煤耗趋势（按月汇总）
     * @return 只包含年份、月份、当月总耗煤量、去年同期总耗煤量的DTO列表（按月汇总）
     */
    @Override
    public List<CoalConsumeTrendDto> prodoperCoalConsume() {
        // 获取当前年份
        int currentYear = LocalDate.now().getYear();
        
        // 查询当年数据（date_type=1表示月数据）
        QueryWrapper<ElectricProdoperCoalConsume> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("year", currentYear)
                .eq("date_type", 1)  // 只查询月数据
                .orderByAsc("month");
        
        List<ElectricProdoperCoalConsume> monthDataList = this.list(queryWrapper);
        
        // 如果没有数据，返回空列表
        if (monthDataList == null || monthDataList.isEmpty()) {
            return new java.util.ArrayList<>();
        }
        
        // 按月份分组，对每个月的数据求和
        java.util.Map<Integer, List<ElectricProdoperCoalConsume>> groupedByMonth =
                monthDataList.stream()
                        .collect(Collectors.groupingBy(ElectricProdoperCoalConsume::getMonth));
        
        // 构建返回结果（按月汇总）
        List<CoalConsumeTrendDto> result = new java.util.ArrayList<>();
        for (java.util.Map.Entry<Integer, List<ElectricProdoperCoalConsume>> entry : groupedByMonth.entrySet()) {
            Integer month = entry.getKey();
            List<ElectricProdoperCoalConsume> monthList = entry.getValue();
            
            CoalConsumeTrendDto dto = new CoalConsumeTrendDto();
            dto.setYear(currentYear);
            dto.setMonth(month);
            
            // 汇总当月耗煤量（多个电厂的数据求和）
            java.math.BigDecimal totalCoalConsumption = monthList.stream()
                    .map(ElectricProdoperCoalConsume::getCoalConsumption)
                    .filter(java.util.Objects::nonNull)
                    .reduce(java.math.BigDecimal.ZERO, java.math.BigDecimal::add);
            
            // 汇总去年同期耗煤量（多个电厂的数据求和）
            java.math.BigDecimal totalLastYearCoalConsumption = monthList.stream()
                    .map(ElectricProdoperCoalConsume::getLastYearCoalConsumption)
                    .filter(java.util.Objects::nonNull)
                    .reduce(java.math.BigDecimal.ZERO, java.math.BigDecimal::add);
            
            dto.setCoalConsumption(totalCoalConsumption);
            dto.setLastYearCoalConsumption(totalLastYearCoalConsumption);
            
            result.add(dto);
        }
        
        // 按月份排序
        result.sort(java.util.Comparator.comparing(CoalConsumeTrendDto::getMonth));
        
        return result;
    }

}
